package roma.home.page;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;

import roma.home.data.Data;

public class AbstractPage {
	
	protected WebDriver driver;
	
	public AbstractPage(WebDriver driver) {
		this.driver = driver;
	}
	
	//---GO TO LINK & ACTIONS
	public void loadHomePage() {
		driver.navigate().to(Data.HOME_URL);
	}

	public void clickOn(WebElement el) {
		el.click();
	}
	
	public void backToPage() {
		driver.navigate().back();
	}
	
	//---ASSERTIONS
	public void checkTitle(String currTitle) {
		String actTitle = driver.getTitle();
		Assert.assertTrue(actTitle.contains(currTitle));
	}
	
	public void checkText(WebElement el ,String currText) {
		String actText = el.getText();
		Assert.assertTrue(actText.contains(currText));
	}
	
	//---MAXIMIZE & WAITS & SCREENSHOTS
	public void maximizeWindow() {
		driver.manage().window().maximize();
	}
	
	public void waitFiveSec() {
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	
	public void waitThreeSec() {
		driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
	}
	
	public void makeScreenShot(String methodName) throws IOException {
		File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
		FileUtils.copyFile(scrFile, new File(Data.filePath + methodName + ".png"));
		System.out.println("***Placed screen shot in " + Data.filePath + " ***");
	}
	
	//---DRAGGING & DROPPING
	public void draggingElement(WebElement el, int x, int y) {
		Actions actions = new Actions(driver);
		actions.dragAndDropBy(el, x, y).perform();
	}
	
	//---SEND TEXT
	public void sendTextToField(WebElement el, String text) {
		el.sendKeys(text);
	}
	
	//---DELETE COOCKIES
	public void deleteCoockies() {
		driver.manage().deleteAllCookies();
	}

}
